package me.zhengjie.controller.material;

import cn.hutool.core.bean.BeanUtil;
import me.zhengjie.dao.MaterialDao;
import me.zhengjie.pojo.entity.NestingMiddleTable;
import me.zhengjie.server.MaterialService;
import me.zhengjie.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/material")
public class MaterialController {

    private static final Logger mergeTaskLog = LoggerFactory.getLogger("material");

    @Resource
    private MaterialService materialService;

    @RequestMapping(value = "/findMaterialByItemBarCode", method = RequestMethod.GET)
    @ResponseBody
    public Result<MaterialDao> findMaterialByItemBarCode(String itemBarCode) {
        MaterialDao MaterialDao = materialService.findDataOne(itemBarCode);
        if (MaterialDao == null) {
            return Result.error("未查询到物料信息");
        }
        return Result.success(MaterialDao);
    }

    @RequestMapping(value = "/findMaterialByContainerCode", method = RequestMethod.POST)
    @ResponseBody
    public Result<List<MaterialDao>> findMaterialByContainerCode(@RequestBody String containerCode) {
        if (containerCode == null) {
            return Result.error("载具码为空！");
        }
        List<NestingMiddleTable> nestingMiddleTables = new ArrayList<>();


        List<MaterialDao> materialDao = materialService
                .findDataDao(new MaterialDao().setContainerCode(containerCode));
        if (materialDao.isEmpty()) {
            return Result.error("未查询到物料信息");
        }
        for (MaterialDao dao : materialDao) {
            BeanUtil.copyProperties(dao,nestingMiddleTables);
        }
        mergeTaskLog.info("数据打印：{}",nestingMiddleTables);
        return Result.success(materialDao);
    }


    @RequestMapping(value = "/findMaterialByContainerCodeGet", method = RequestMethod.GET)
    @ResponseBody
    public Result<List<MaterialDao>> findMaterialByContainerCodeGet(String containerCode) {
        if (containerCode == null) {
            return Result.error("载具码为空！");
        }
        List<MaterialDao> materialDao = materialService
                .findDataDao(new MaterialDao().setContainerCode(containerCode));
        if (materialDao.isEmpty()) {
            return Result.error("未查询到物料信息");
        }
        return Result.success(materialDao);
    }
}
